﻿/* 
    Jolt Environment
    Copyright (C) 2010 Jolt Environment Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RuneScape.Model.Items;
using RuneScape.Model.Items.Containers;

namespace RuneScape.Communication.Messages.Outgoing
{
    /// <summary>
    /// Composes a set of given items to the desired interface.
    /// </summary>
    public class InterfaceItemsPacketComposer : PacketComposer
    {
        #region Constructors
        /// <summary>
        /// Constructs the packet.
        /// </summary>
        public InterfaceItemsPacketComposer(short interfaceId, short childId, short type, Container container)
        {
            SetOpcode(255);
            SetType(PacketType.Short);
            AppendShort(interfaceId);
            AppendShort(childId);
            AppendShort(type);
            AppendShort(container.Capacity);

            for (int i = 0; i < container.Capacity; i++)
            {
                Item item = container[i];
                short id;
                int count;

                if (item == null)
                {
                    id = -1;
                    count = 0;
                }
                else
                {
                    id = item.Id;
                    count = item.Count;
                }

                if (count > 254)
                {
                    AppendByteS(255);
                    AppendIntTertiary(count);
                }
                else
                {
                    AppendByteS((byte)count);
                }
                AppendLEShort((short)(id + 1));
            }
        }
        #endregion Constructors
    }
}
